﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using weikebuluo.DAL;
using weikebuluo.Model;
using weikebuluo.Common;

namespace weikeweixin.web.admin.website
{

    public partial class webset : weikeweixin.web.admin.cs.adminbase
    {
        t_set_dal cMerchantDAL = new t_set_dal();
        t_set_model cMerchantModel = new t_set_model();

        public string MName;
        public string Description;
        public string HomeShareImg;
        public string HomeBgImg;
        public string CoverImg;
        public string MPhone;
        public string Province;
        public string City;
        public string Area;
        public string CopyRight;
        public string Template;
        public string KeyWords;
        public string FontColor;

        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dtMerchant = cMerchantDAL.GetList("user_id ='" + user_id + "'").Tables[0];

            //填写过，修改    
            if (dtMerchant.Rows.Count > 0)
            {
                MName = dtMerchant.Rows[0]["MName"].ToString();
                Description = dtMerchant.Rows[0]["Description"].ToString();
                MPhone = dtMerchant.Rows[0]["MPhone"].ToString();
                Province = dtMerchant.Rows[0]["Province"].ToString();
                City = dtMerchant.Rows[0]["City"].ToString();
                Area = dtMerchant.Rows[0]["Area"].ToString();
                KeyWords = dtMerchant.Rows[0]["KeyWords"].ToString();
                CopyRight = dtMerchant.Rows[0]["CopyRight"].ToString();
                Template = dtMerchant.Rows[0]["Template"].ToString();
                HomeShareImg = dtMerchant.Rows[0]["HomeShareImg"].ToString();
                HomeBgImg = dtMerchant.Rows[0]["HomeBgImg"].ToString();
                CoverImg = dtMerchant.Rows[0]["CoverImg"].ToString();
                FontColor = dtMerchant.Rows[0]["FontColor"].ToString();
            }
            //第一次填写
            else
            {   
                MName = "";
                Description = "";
                MPhone = "";
                Province = "江西省";
                City = "南昌市";
                Area = "市辖区";
                KeyWords = "";
                CopyRight = "微客网络工作室";
                HomeShareImg = "/web/admin/website/styles/images/home-default21.jpg";
                HomeBgImg = "/web/admin/website/styles/images/home-default21.jpg";
                CoverImg = "/web/admin/website/styles/images/home-default21.jpg";
                FontColor = "#F2F2F2";
            }
            switch (Request.Form["action"])
            {
                case "bindtmerchant"://商户设置
                    SubmitTMerchant();
                    break;
            }
        }

        #region 提交官网信息
        public void SubmitTMerchant()
        {

            MName = Request.Form["title"].Trim();
            CoverImg = Request.Form["cover"].Trim();
            Description = Request.Form["brief"].Trim();
            MPhone = Request.Form["phone"].Trim();
            Province = Request.Form["location_p"].Trim();
            City = Request.Form["location_c"].Trim();
            Area = Request.Form["location_a"].Trim();
            KeyWords = Request.Form["keyword"].Trim();
            CopyRight = Request.Form["copyright"].Trim();
            HomeShareImg = Request.Form["logo"].Trim();
            HomeBgImg = Request.Form["show_bg_img"].Trim();
            FontColor = Request.Form["cname_color"].Trim();

            DataTable dtMerchant = cMerchantDAL.GetList("user_id ='" + user_id + "'").Tables[0];

            if (dtMerchant.Rows.Count > 0)
            {
                //提交过，修改
                cMerchantModel = cMerchantDAL.GetModel(Convert.ToInt32(dtMerchant.Rows[0]["MId"]));

                cMerchantModel.MName = MName;
                cMerchantModel.CoverImg = CoverImg;
                cMerchantModel.Description = Description;
                cMerchantModel.MPhone = MPhone;
                cMerchantModel.Province = Province;
                cMerchantModel.City = City;
                cMerchantModel.Area = Area;
                cMerchantModel.KeyWords = KeyWords;
                cMerchantModel.CopyRight = CopyRight;
                if (string.IsNullOrEmpty(cMerchantModel.Template.ToString())) {
                    cMerchantModel.Template = 1; 
                }
                cMerchantModel.HomeShareImg = HomeShareImg;
                cMerchantModel.HomeBgImg = HomeBgImg;
                cMerchantModel.FontColor = FontColor;
                cMerchantDAL.Update(cMerchantModel);
                Response.Write("{\"errno\":\"0\",\"tip\":\"微官网设置成功！\",\"url\":\"webset.aspx\",\"error\":\"\"}");
                Response.End();
            }
            else 
            //第一次填写
            {

                //Merchant表提交
                cMerchantModel.MName = MName;
                cMerchantModel.CoverImg = CoverImg;
                cMerchantModel.Description = Description;
                cMerchantModel.MPhone = MPhone;
                cMerchantModel.Province = Province;
                cMerchantModel.City = City;
                cMerchantModel.Area = Area;
                cMerchantModel.KeyWords = KeyWords;
                cMerchantModel.CopyRight = CopyRight;
                cMerchantModel.Template = Convert.ToInt32(Template);
                cMerchantModel.HomeShareImg = HomeShareImg;
                cMerchantModel.HomeBgImg = HomeBgImg;
                cMerchantModel.Template = 1;
                cMerchantModel.FontColor = FontColor;
                cMerchantModel.user_id = int.Parse(user_id);
                cMerchantDAL.Add(cMerchantModel);

                //MerchantUser表提交
                Response.Write("{\"errno\":\"0\",\"tip\":\"微官网设置成功！\",\"url\":\"webset.aspx\",\"error\":\"\"}");
                Response.End();
            }
        }
        #endregion

    }
}